package com.javafactroy.kunlong.codefactroy.service.tool;


import com.javafactroy.kunlong.base.config.datasource.DatasourceConfig;
import com.javafactroy.kunlong.base.config.datasource.postgres.PostgresDatasourceConfig;
import com.javafactroy.kunlong.base.metadata.model.TMetadataModelPostgres;
import com.javafactroy.kunlong.base.metadata.valueobj.MetadataTable;
import com.javafactroy.kunlong.codefactroy.consts.IdbType;
import com.javafactroy.kunlong.codefactroy.convert.PostgresTablesConvert;
import com.javafactroy.kunlong.data.entity.mysql.MysqlTables;
import com.javafactroy.kunlong.data.entity.postgres.PostgresTables;
import com.javafactroy.kunlong.codefactroy.service.base.FileTemplateFactroy;
import com.javafactroy.kunlong.codefactroy.service.domain.valueobj.EntityProps;

import com.javafactroy.kunlong.data.dao.postgres.PostgresTablesMapper;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
@ConditionalOnProperty(prefix = "spring.datasource.postgres", name = "enabled", havingValue = "true")

public class DbCodePostgresTool {
    Boolean rmFile = true;
    @Autowired
    TMetadataModelPostgres metadataModel;

    @Autowired
    PostgresTablesConvert postgresTablesConvert;
    @Autowired
    FileTemplateFactroy fileTemplateFactroy;
    @Autowired
    PostgresDatasourceConfig postgresDatasourceConfig;

    @Autowired
    PostgresTablesMapper postgresTablesMapper;

    void make(DatasourceConfig datasourceConfig){
        if(datasourceConfig.getDbName().equals(IdbType.DbType_PostGres)) {
            makes();
        }
    }

    public void makes() {
        //information_schema.TABLES

        loadTables().forEach(i -> make(i.getTableName()));
    }

    public void make(String table) {

        MetadataTable metadata = metadataModel.loadDbPostgresMetadata(table);
        makeFile(metadata);
    }

    void makeFile(MetadataTable metadata) {
        EntityProps entityProps = metadata.toEntityProps();
        entityProps.makeParams(IdbType.DbType_PostGres);
        log.info("template params={}", CommonUtils.toJSONString(entityProps.getStringTemplate().getParams()));

        if (rmFile) {
            fileTemplateFactroy.rmDbFile(metadata);
            rmFile = false;
        }
        fileTemplateFactroy.getList().forEach(i -> {

            String stext = i.valueOf(entityProps).parse(i.readFile());
            i.writeFile(stext);
        });

    }


    List<MysqlTables> loadTables() {
        String dbName = postgresDatasourceConfig.getDbName();
        List<PostgresTables> tables = postgresTablesMapper
                .select(new PostgresTables().setDbName("public").setTableCatalog(dbName));
        log.info("tables = {}", CommonUtils.toJSONString(tables));

        return postgresTablesConvert.fromList(tables);
    }

}
